<template>
  <view id="page">
    <view class="edit bg-white">
      <text-area :height="250" :maxlength="500" class="text-area" placeholder="这一刻的想法..."></text-area>
      <div style="margin-top: 10px;border-top: 1px solid #f0f0f0;padding: 10px 16px 16px 16px;"></div>
      <image-card class="image-card"></image-card>

      <view class="cells flex-row">
        <view class="cell flex-row">
          <text class="cell-icon iconfont icon-line"></text>
          <navigator url="./subpage/viewPublishedDynamic/viewPublishedDynamic" class="cell-wrap flex-row">
            <text class="cell-label" @click="clicks">查看已发布的动态</text>
            <text class="arrow-icon iconfont icon-lr"></text>
          </navigator>
        </view>
      </view>
    </view>
    <view class="btn-wrap">
      <button class="btn btn-submit" @click="onPush">发 布</button>
    </view>
  </view>


</template>
<script>
import {addTrends} from '@/api/trends'
import {getToken} from "@/utils/auth";
import {mapGetters} from 'vuex'
import Bus from "@/utils/bus";

export default {
  name: '',
  components: {},
  data() {
    return {
      fileList: []
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  mounted() {
  },
  created() {
  },
  methods: {
     clicks(){
      wx.navigateTo({
        url: '/pages/circle/dynamic/main'
      });
    },
    async onPush() {
      let that = this;
      let content = this.$root.$mp.page.selectComponent(".text-area").data.content
      let images = this.$root.$mp.page.selectComponent(".image-card").data.images;
      console.log(images)
      if (!content.trim().length && images.length <= 0) {
        this.setToast({title: "请填写动态内容"});
        return false;
      }
      wx.showLoading({title: "正在发布"});
      /*for (let i = 0; i < images.length; i++) {
        console.log(images[i])
        await wx.uploadFile({
          // url: 'http://42.192.211.158:8888/zutassociation/common/upload', // 仅为示例，非真实的接口地址
          url: 'http://localhost:8888/zutassociation/common/upload',
          filePath: images[i],
          name: 'file',
          headers: {Authorization: "Bearer " + getToken()},
          // formData: {
          //   //和服务器约定的token, 一般也可以放在header中
          //   'session_token': wx.getStorageSync('session_token')
          // },
          success(res) {
            let data = JSON.parse(res.data)
            if (data.code !== 200) {
              this.setModal({content: '图片上传失败'})
              return;
            }
            let currentFile = {fileName: data.fileName, url: data.url};
            that.fileList.push(currentFile)
          },
          fail: function (e) {
            console.log(e);
            this.setModal({content: '图片上传失败'})
          },
          complete: function () {
            wx.hideToast();  //隐藏Toast
            // wx.hideLoading();
            // this.fileList = wx.getStorageSync('list')
          }
        });
      }
      console.log(that.fileList)*/


      let data = {
        remark: content,
        userId: JSON.parse(JSON.stringify(this.user.userId)),
        img: JSON.stringify(images)
      }
      addTrends(data).then(res => {
        if (res.code === 200) {
          data={}
          /*wx.redirectTo({
            url: "./subpage/viewPublishedDynamic/viewPublishedDynamic"
          });*/
          Bus.$emit('跳转刷新', 'onSave');
          wx.navigateBack({ delta: 1 });
        } else {
          this.setToast({title: "发布失败！"});
        }
      }).catch(res => {
        console.log(res);
        this.setToast({title: "发布失败！"});
      }).then(() => {
        wx.hideLoading();
      });

    }
  }
}


</script>
<style scoped>
.edit {
  padding: 15px;
}

.cells {
  width: 95%;
  margin: 0 auto;
  margin-top: 25px;
  padding: 15px;
  justify-content: space-between;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
  box-sizing: border-box;
}

.cell {
  width: 90%;
  margin: 0 auto;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
}

.cell-wrap {
  width: 100%;
  align-items: center;
  justify-content: space-between;
  padding: 15px 5px;
  border-bottom: 1px solid #f0f0f0;
}

.cell-icon {
  margin-right: 10px;
  font-size: 25px;
}

.arrow-icon {
  margin-left: 7.5px;
  font-size: 14px;
  color: #afafaf;
}

.btn-wrap {
  position: fixed;
  bottom: 0;
}

</style>
